$(document).ready(function(){	
	$("#modifyUser").val(username);	
	initCate();
	
    //获取当前父亲Id
	var src = tl_config.his_url;
	var i = src.indexOf('?');
	if(i!=-1){
		var id = src.substring(i+4);
        $.ajax({
        	url:server_url+'/rest/navigation/selectNavigationById.do?ran='+Math.random()+ '&token=' + token,
        	type:'get',
        	data:{id:id},
        	success:function(data){
        		if(data.retCode==='0000'){        			
        			$("#id").val(data.data.id);
        			$("#parentNvId").val(data.data.parentNvId);
        			$("p#parentMsortName").text(data.data.parentMsortName);
        			$("#tsnId").val(data.data.tsnId);
        			$("#tsnName").val(data.data.tsnName);        			
        			$("#sort").val(data.data.sort);        		
        		}
        	}
        });
	}	
	
	//表单验证
	var validate = $("#fromEditNavigation").validate({		   
		rules:{					
			tsnName:{
				  required:true,
			      remote: { 
	                   url: server_url+'/rest/navigation/checkTsnNameEdit.do?timestamp='+Math.random()+ '&token=' + token,
	                   type: "post",
	                   dataType: "json", 
	                   data: {	id: function() { return $("#id").val();},	                	        
	                	        tsnName: function() { return $("#tsnName").val();},
	                	        parentNvId: function() { return $("#parentNvId").val();}
	                         } 
	               },
	               stringMaxLength:30
			}
		},
		messages:{				
			tsnName:{
			           required:"该值不能为空！",
                       remote:"该分类名已存在，请重新输入!",
                       stringMaxLength:"最多能输入30个字符！"
                   }
		},
		submitHandler:function(){
			sub();
		},
		errorPlacement: function(error, element) {
			if ( element.is(":radio") )
				error.appendTo ( element.parent() );
			else if ( element.is(":checkbox") )
				error.appendTo ( element.parent() );
			else if ( element.is("input[name=captcha]") )
				error.appendTo ( element.parent() );
			else
				error.insertAfter(element);
		},
		validClass : 'rightValidate',
	    success: function(label) {
		   label.html("&nbsp;").text(" ") ;
	    }
 });	

	
});


//提交
function sub(){
	if($("#fromEditNavigation").valid()){
		$.ajax({
			url:server_url+'/rest/navigation/NavigationEdit.do?ran='+Math.random()+ '&token=' + token,
			type:'post',
			data:$("#fromEditNavigation").serialize(),
			success:function(data){
				easyDialog.open({container:{header:'温馨提示',content:data.retMsg},autoClose : 2000});
				back();
			}
		});
	}
}


function back(){
	var url="getNavigationListPage.do?ran="+Math.random();
	tl_config.loadToWorkArea(url);
}

function submitCommit(){
	$("#fromEditNavigation").submit();
}



//开窗选择分类
function toOpenPop(){	
	easyDialog.open(
	{
		  container :'bm_box',
		  fixed : false
	}	
	);	
	
}

function closeBtn(){
	$(".l_pageUpDown").remove();  
}

function sureBtn(cateId,cateName){
	$("#tsnId").val(cateId);
	$("#tsnName").val(cateName);
	easyDialog.close();
}

function initCate(){	
	gridObj = $.fn.bsgrid.init('g_tabYeh', {
		ajaxType : 'get',
		url :  server_url+'/rest/productCate/productCateList.do?ran=' + Math.random() + '&token=' + token,
		autoLoad : true,
		pageSizeSelect : true,
		stripeRows : true,
		pageSize : 10,
		displayBlankRows : false,
		isProcessLockScreen : true,
		rowSelectedColor : false,
		showPageToolbar:true
	});
}

function operate(record, rowIndex, colIndex, options) {	
	return '<a href="javascript:sureBtn(\'' + gridObj.getRecordIndexValue(record, 'cateId') + '\''+'\,\''+ gridObj.getRecordIndexValue(record, 'cateName') +'\');">确认选择</a>';
}
